﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Web;
using Entities.BookStore;
using System.Xml.Serialization;
using System.Runtime.Serialization.Json;
using Services.BookStore;

namespace Client
{
    class Program
    {
        static string baseUrl = "http://localhost:2012/StoreService.svc/";

            
        static void Main(string[] args)
        {
            Uri http = new Uri(string.Concat(Program.baseUrl));
            var proxy = new WebChannelFactory<IBookStoreServiceContract>(http);
            var methods = proxy.CreateChannel();
            methods.InsertBook(new Book() { Copies = 300, Title = "a" });

            methods.UpdateBook(new Book() { Id = 1, Copies = 3100, Title = "WWWW" });

            var a = methods.GetBooks();

            InsertBook();
            InsertBook();
            InsertBook();
            InsertBook();
            InsertBook();
            InsertBook();

            GetBooks();

            UpdateBook();

            GetBooks();                  
            DeleteBook();
            
            GetBooks();
            FindBook();

            Console.ReadLine();


        }

        static void UpdateBook()
        {
            string bookUrl = string.Concat(baseUrl, "UpdateBook");

            var webClient = new WebClient();
            webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");
            /*
            webClient.UploadDataCompleted +=
                new UploadDataCompletedEventHandler(delegate(object sender, UploadDataCompletedEventArgs e)
                {
                    DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(ResponseBook));
                    var res = js.ReadObject(new MemoryStream(e.Result)) as ResponseBook;
                    Console.WriteLine("\n************\nUpdate({0}) - {1}\n************\n", res.IsOk, res.Message);
                   
                    
                });
            */
            Book book = new Book();
            book.Title = "MARIA";
            book.Id = 1;
            DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Book));
            using (var ms = new MemoryStream())
            {
                json.WriteObject(ms, book);
                var Result = webClient.UploadData(new Uri(bookUrl), "PUT", ms.ToArray());
                DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(ResponseBook));
                var res = js.ReadObject(new MemoryStream(Result)) as ResponseBook;
                Console.WriteLine("\n************\nUpdate({0}) - {1}\n************\n", res.IsOk, res.Message);


            }
        }


        static void DeleteBook()
        {
            string bookUrl = string.Concat(baseUrl, "DeleteBook");

            var webClient = new WebClient();
            webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");

            int Id = 1;
            DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(int));
            using (var ms = new MemoryStream())
            {
                json.WriteObject(ms, Id);
                var Result = webClient.UploadData(new Uri(bookUrl), "DELETE", ms.ToArray());
                DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(ResponseBook));
                var res = js.ReadObject(new MemoryStream(Result)) as ResponseBook;
                Console.WriteLine("\n************\nDelete({0}) - {1}\n************\n", res.IsOk, res.Message);


            }
        }

        static void InsertBook()
        {
            string bookUrl = string.Concat(baseUrl, "InsertBook");

            var webClient = new WebClient();
            webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");
            /*
            webClient.UploadDataCompleted +=
                new UploadDataCompletedEventHandler(delegate(object sender, UploadDataCompletedEventArgs e)
                {
                    DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Response));
                    var res = js.ReadObject(new MemoryStream(e.Result)) as Response;
                    Console.WriteLine("\n************\nInsert({0}) - {1}\n************\n", res.IsOk, res.Message);
                     
           
                });
            */
            Book book = new Book();
            string[] n = new string[] { "ANTONIO","PEDRO","AMADEUS","MANUEL","MARIA","MARA","LUQUET"};
            book.Title = n[new Random().Next(n.Length-1)];
            DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Book));
            using (var ms = new MemoryStream())
            {
                json.WriteObject(ms, book);
                var result  = webClient.UploadData(new Uri(bookUrl), "POST", ms.ToArray());
                DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(Response));
                var res = js.ReadObject(new MemoryStream(result)) as Response;
                Console.WriteLine("\n************\nInsert({0}) - {1}\n************\n", res.IsOk, res.Message);
                   
            }
        }

        static void GetBooks()
        {
            string booksUrl = string.Concat(baseUrl, "GetBooks");


            var webClient = new WebClient();
            /*
             webClient.OpenReadCompleted +=
             new OpenReadCompletedEventHandler(delegate(object sender, OpenReadCompletedEventArgs e)
             {
                 using (var streamReader = new System.IO.StreamReader(e.Result))
                 {
                     Console.WriteLine(streamReader.ReadToEnd());
                    
                 }
             });
             */
            var Result = webClient.OpenRead(new Uri(booksUrl));
            using (var streamReader = new System.IO.StreamReader(Result))
            {
                Console.WriteLine("GetBooks - {0}", streamReader.ReadToEnd());

            }

        }

        static void FindBook()
        {
            string booksUrl = string.Concat(baseUrl, "FindBook?Id=2");


            var webClient = new WebClient();
            /*
             webClient.OpenReadCompleted +=
             new OpenReadCompletedEventHandler(delegate(object sender, OpenReadCompletedEventArgs e)
             {
                 using (var streamReader = new System.IO.StreamReader(e.Result))
                 {
                     Console.WriteLine(streamReader.ReadToEnd());
                    
                 }
             });
             */
            var Result = webClient.OpenRead(new Uri(booksUrl));
            using (var streamReader = new System.IO.StreamReader(Result))
            {
                Console.WriteLine("FindBook - {0}",streamReader.ReadToEnd());

            }

        }
    }
}
